package com.szhuang.mobilesafe.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.service.AddressService;
import com.szhuang.mobilesafe.tools.ServiceUtils;
import com.szhuang.mobilesafe.ui.SettingClickView;
import com.szhuang.mobilesafe.ui.SettingItemView;

/**
 * Created by szhuang on 2015/3/21.
 */
public class SettingActivity extends Activity {

    private SharedPreferences sp;
    private SettingItemView updateItem;
    private TextView aboutSoftware;
    private SettingItemView show_number_address;
    private Intent showNumberAddress;
    private SettingClickView diy_contacts_toast_bg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();
        setListener();

    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean isServiceRunning= ServiceUtils.isServiceRunning(SettingActivity.this,"com.szhuang.mobilesafe.service.AddressService");
        if(isServiceRunning){
            show_number_address.setChecked(true);
            show_number_address.setDesc("号码归属地显示已经开启");
        }else{
            show_number_address.setChecked(false);
            show_number_address.setDesc("号码归属地显示没有开启");
        }
    }

    public void init(){
        sp=getSharedPreferences("config",MODE_PRIVATE);
        updateItem=(SettingItemView)findViewById(R.id.updateItem);
        aboutSoftware=(TextView)findViewById(R.id.aboutSoftware);
        show_number_address= (SettingItemView) findViewById(R.id.siv_show_number_address);
        diy_contacts_toast_bg= (SettingClickView) findViewById(R.id.diy_contacts_toast_bg);
        showNumberAddress=new Intent(this, AddressService.class);

        updateItem.setTitle("自动更新是否开启");
        if(sp.getBoolean("update",true)){
            updateItem.setChecked(true);
            updateItem.setDesc("自动更新已经开启");
        }else{
            updateItem.setChecked(false);
            updateItem.setDesc("自动更新没有关闭");
        }

        boolean openNumberAttribution=sp.getBoolean("openNumberAttribution",false);
        if(openNumberAttribution){
            startService(showNumberAddress);
        }
        show_number_address.setTitle("号码归属地显示是否开启");
        boolean isServiceRunning= ServiceUtils.isServiceRunning(this, "com.szhuang.mobilesafe.service.AddressService");
        if(isServiceRunning){
            show_number_address.setChecked(true);
            show_number_address.setDesc("号码归属地显示没有开启");
        }else{
            show_number_address.setChecked(false);
            show_number_address.setDesc("号码归属地显示已经开启");
        }
        int which=sp.getInt("which",0);
        diy_contacts_toast_bg.setTitle("选择来电归属地对话框背景");
        switch (which){
            case 0:
                diy_contacts_toast_bg.setDesc("灰色");
                break;
            case 1:
                diy_contacts_toast_bg.setDesc("橘色");
                break;
            case 2:
                diy_contacts_toast_bg.setDesc("深橘色");
                break;
        }

    }

    public void setListener(){
        updateItem.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor=sp.edit();
                if(updateItem.isOpen()){
                    updateItem.setChecked(false);
                    updateItem.setDesc("自动更新已经关闭");
                    editor.putBoolean("update",false);
                }else{
                    updateItem.setChecked(true);
                    updateItem.setDesc("自动更新已经开启");
                    editor.putBoolean("update",true);
                }
                editor.commit();
            }
        });

        aboutSoftware.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SettingActivity.this,AboutSoftwareActivity.class);
                startActivity(intent);
            }
        });

        show_number_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isServiceRunning= ServiceUtils.isServiceRunning(SettingActivity.this,"com.szhuang.mobilesafe.service.AddressService");
                SharedPreferences.Editor editor=sp.edit();
                if(isServiceRunning){
                    show_number_address.setChecked(true);
                    show_number_address.setDesc("号码归属地显示已经开启");
                }else{
                    show_number_address.setChecked(false);
                    show_number_address.setDesc("号码归属地显示没有开启");
                }
                if(show_number_address.isOpen()){
                    show_number_address.setChecked(false);
                    stopService(showNumberAddress);
                    show_number_address.setDesc("号码归属地显示没有开启");
                    editor.putBoolean("openNumberAttribution",false);
                }else{
                    show_number_address.setChecked(true);
                    startService(showNumberAddress);
                    show_number_address.setDesc("号码归属地显示已经开启");
                    editor.putBoolean("openNumberAttribution",true);
                }
                editor.commit();
            }
        });
        diy_contacts_toast_bg.setOnClickListener(new View.OnClickListener() {
            final String [] items = {"灰色","橘色","深橘色"};
            @Override
            public void onClick(View v) {
                int selected=sp.getInt("which",0);
                final AlertDialog.Builder builder=new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("归属地提示框风格");

                builder.setSingleChoiceItems(items,selected,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        SharedPreferences.Editor editor=sp.edit();
                        editor.putInt("which",which);
                        editor.commit();
                        diy_contacts_toast_bg.setDesc(items[which]);
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("cancel",null);
                builder.show();

            }
        });
    }
}
